<?php
namespace app\controllers\api\mail;

use fate\http\Request;
use app\libs\Validator;
use app\services\MailService;
use app\libs\Util;
use app\models\User;

class SendregisterController extends \fate\web\PureController {

    public function run() {
        $req = Request::getInstance();

        $email = Util::plainText($req->getParameter('email'));

        // 邮箱合法性
        if(!Validator::validateEmail($email)) {
            $this->ajaxReturn(null, 100003, '错误的邮箱');
            exit;
        }

        if(!Validator::permittedEmail($email)) {
            $this->ajaxReturn(null, 100003, '不允许的邮箱');
            exit;
        }

        $user = new User();
        // 邮箱是否已经存在
        $has = $user->count("email='". $email ."'");
        if($has > 0) {
            $this->ajaxReturn(null, 100006);
            exit;
        }

        $mail = new MailService();
        $rs = $mail->sendRegisterCode($email);

        if($rs) {
            $this->ajaxReturn(null);
            exit;
        }

        $this->ajaxReturn(null, 100005, $mail->msg);
    }

}
